package Lab07;

public class Test {
}
//
//class AA {
//    public AA() {
//        System.out.println("A's no-arg constructor is invoked");
//    }
//}
//
//class BB extends AA {
//}
//
//class C {
//    public static void main(String[] args) {
//        BB b = new BB();
//    }
//}
//
//class AA {
//    public AA(int x) {
//    }
//}
//
//class BB extends AA {
//    public BB() {
//    }
//}
//
//class C {
//    public static void main(String[] args) {
//        BB b = new BB();
//    }
//}


//Exception in thread "main" java.lang.ClassCastException: class Lab07.Fruit
// cannot be cast to class Lab07.Apple (Lab07.Fruit and Lab07.Apple are in unnamed module of loader 'app')

class TestApple {
    public static void main(String[] args) {
        Object fruit = new Fruit();
//        Object apple =  (Apple) fruit;
//        https://blog.csdn.net/qq_38006520/article/details/87189091
//        父类不能转化为子类，什么意思呢，就是说new出来一个Fruit，那他仅仅是一个Fruit
//        他不是apple，这个Fruit可以是香蕉或者甘蔗，那他们肯定和apple不是一个东西，那肯定
//        不能转化为apple了
//        但是如果这样写
        Fruit fruitApple = new Apple();
        Apple fruitAppleToAppleObj = (Apple) fruitApple;
//        虽然我们用一个Fruit去接这个fruitApple，但是他本质上还是个Apple，我们只是
//        先把他当作Fruit，忽略他作为apple的一些特性。当我们要用apple的一些特性的时候
//        我们可以  Apple fruitAppleToAppleObj = (Apple) fruitApple;
//        把他强制转化为一个apple，这就可以把他当apple用了

//        举个极端的例子
//        Apple apple1 =  (Apple) fruit;

        testInstanceof();
    }

    static void testInstanceof() {
        Fruit fruit1 = new GoldenDelicious();
        Orange orange = new Orange();

        //没有cast
//      Macintosh p = new Apple();
        System.out.println("fruit1 instanceof  Fruit: " + (fruit1 instanceof Fruit));
        System.out.println("fruit1 instanceof  Orange:" + (fruit1 instanceof Orange));
        System.out.println("fruit1 instanceof Apple: " + (fruit1 instanceof Apple));
        System.out.println("fruit1 instanceof GoldenDelicious:" + (fruit1 instanceof GoldenDelicious));
        System.out.println("fruit1 instanceof  Macintosh: " + (fruit1 instanceof Macintosh));
        System.out.println("orange instanceof Orange: " + (orange instanceof Orange));
        System.out.println("orange instanceof Fruit: " + (orange instanceof Fruit));
//        System.out.println("orange instanceof  Apple:"+(orange instanceof Apple));
    }
}

class Apple extends Fruit {

    void makeAppleCider() {
        System.out.println("makeAppleCider");
    }
}

class Fruit {
}

class Orange extends Fruit {

}

class GoldenDelicious extends Apple {

}

class Macintosh extends Apple {

}

//
//
//
//class Fruit {
//
//}
//
//class Apple {
//
//}
//
//class Macintosh extends Apple {
//
//}
//
//class TestFruit {
//    public static void main(String[] args) {
//        Apple p = new Macintosh();
//    }
//}
//
//
